package com.dcits.ftz.controller;

import com.dcits.annotation.ParamCheck;
import com.dcits.common.Result;
import com.dcits.components.threadpools.ExecutorRegistry;
import com.dcits.ftz.auth.JwtUtil;
import com.dcits.ftz.dto.in.LoginFormIn;
import com.dcits.ftz.entity.User;
import com.dcits.ftz.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kui.liu
 * @since 2025-06-06
 */
@RestController
@RequestMapping("/v1.0/user")
@Slf4j
@EnableAsync
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "用户注册", notes = "用于测试框架，执行时正常返回和业务异常场景测试")
    @GetMapping("/register")
    @ParamCheck
    public Result<Boolean> register() {
//        init();
        return Result.success(userService.insertBatch());
    }

    @Autowired
    private ExecutorRegistry registry;

    @ApiOperation(value = "用户注册", notes = "用于测试框架，执行时正常返回和业务异常场景测试")
    @GetMapping("/page")
    @ParamCheck
    public Result<List<User>> page(@Param("pageNo") int pageNo, @Param("sleep") int sleep) {
        sleep = sleep < 100 ? 100 : sleep;
        final LongAdder sleepTime = new LongAdder();
        sleepTime.add(sleep);
        Executor orderPool = registry.getExecutorMap().get("cnaps-pool");
        LongAdder longAdder = new LongAdder();
        orderPool.execute(() -> {
            // 订单处理逻辑
            longAdder.increment();
//                log.info("<UNK>:{}", longAdder.longValue());
            try {
                TimeUnit.MILLISECONDS.sleep(sleepTime.intValue());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        longAdder.reset();
        return Result.success(userService.page(pageNo));
    }

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/login")
    public Result login(LoginFormIn user) {
// 1. 验证用户名密码 (这里应该从数据库验证)
//        User dbUser = userService.findByUsername(user.getUsername());
//
//        if (dbUser == null || !dbUser.getPassword().equals(user.getPassword())) {
//            throw new RuntimeException("用户名或密码错误");
//        }

        String token = jwtUtil.generateToken(user.getUsername());

        return Result.success("Bearer "+token);
    }

    /*@GetMapping("/logout")
    public Result logout() {
        SecurityUtils.getSubject().logout();
        return Result.success();
    }*/
}
